package com.sbl.bajw.pc.action;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.ExcelUtil;
import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.mapper.OperationLogModelDao;
import com.sbl.bajw.pc.model.LogTModel;
import com.sbl.bajw.pc.model.OperationLogModel;
import com.sbl.bajw.pc.model.collectLogModel;
import com.sbl.bajw.pc.service.LogTModelService;
import com.sbl.bajw.pc.service.collectLogModelService;
import com.sbl.bajw.util.LogUtil;

@Controller
@RequestMapping("/collect")
public class collectLogModelAction {

	@Resource
	private collectLogModelService service;
	
	@Resource
	private LogTModelService logService;
	
	@Resource
	private OperationLogModelDao op_dao;
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	static Logger log;
	
	static{
		log = LogUtil.getLogger("collect");
	}
	
	/*
	 * 日常工作采集
	 */
	@RequestMapping(value = "/create")
	public @ResponseBody String create() throws MessageException{
		collectLogModel model = RequestUnit.getModel(collectLogModel.class);
		String json = "";
		try {
			log.info(model.getCreateBy() + "新建工作日志");
			service.insert(model);
			
			LogTModel lt = new LogTModel();
			lt.setCode("3");
			lt.setUserid(model.getCreateBy());
			lt.setOperatingContent(model.getCreateBy() + "于" + sdf.format(new Date()) + "在" + model.getAddr() + "上传采集日志");
			logService.insert(lt);
			
			json = JsonResultUtil.Success();
		} catch (Exception e) {
			log.info(e.getMessage());
			e.printStackTrace();
			throw new MessageException("新建失败", "3");
		}
		return json;
	}
	
	@RequestMapping(value = "/getCollectLogList")
	public @ResponseBody String getCollectLogList(HttpServletResponse response, HttpServletRequest request){
		collectLogModel model = RequestUnit.getModel(collectLogModel.class);
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(model.getAddr() != null && !model.getAddr().equals("")){
			parm.put("addr", "%" + model.getAddr() + "%");
		}
		if(model.getContent() != null && !model.getContent().equals("")){
			parm.put("content", "%" + model.getContent() + "%");
		}
		if(model.getStart_date() != null && !model.getStart_date().equals("")){
			parm.put("start_date", model.getStart_date() + " 00:00:00");
		}
		if(model.getStop_date() != null && !model.getStop_date().equals("")){
			parm.put("stop_date", model.getStop_date() + " 23:59:59");
		}
		if(model.getUsername() != null && !model.getUsername().equals("")){
			parm.put("username", "%" + model.getUsername() + "%");
		}
		String json = service.getCollectLogList(parm);
		return json;
	}
	
	@RequestMapping(value = "/downloadCollectLogList")
	public void downloadCollectLogList(HttpServletResponse response, HttpServletRequest request) throws Exception{
		collectLogModel model = RequestUnit.getModel(collectLogModel.class);
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(model.getAddr() != null && !model.getAddr().equals("")){
			parm.put("addr", "%" + model.getAddr() + "%");
		}
		if(model.getContent() != null && !model.getContent().equals("")){
			parm.put("content", "%" + model.getContent() + "%");
		}
		if(model.getStart_date() != null && !model.getStart_date().equals("")){
			parm.put("start_date", model.getStart_date() + " 00:00:00");
		}
		if(model.getStop_date() != null && !model.getStop_date().equals("")){
			parm.put("stop_date", model.getStop_date() + " 23:59:59");
		}
		if(model.getUsername() != null && !model.getUsername().equals("")){
			parm.put("username", "%" + model.getUsername() + "%");
		}
		List<HashMap<String, Object>> list = service.downloadCollectLogList(parm);
		
		HashMap<String, String> column = new HashMap<String, String>();
		column.put("ID", "编号");
		column.put("ADDR", "日志发布地址");
		column.put("CONTENT", "日志内容");
		column.put("MAPX", "坐标经度");
		column.put("MAPY", "坐标纬度");
		column.put("SHOWNAME", "日志发布人");
		column.put("CREATE_TIME", "发布时间");
		
		HSSFWorkbook wb = ExcelUtil.createExcel(list, column);
		OutputStream outStream = response.getOutputStream();
		response.setContentType("application/x-msdownload");  
		response.setHeader("Content-Disposition","attachment;filename=collect_log.xls");  
		wb.write(outStream);
	}
	
	@RequestMapping(value = "/getCollectLogDetails")
	public ModelAndView getCollectLogDetails(HttpServletResponse response, HttpServletRequest request){
		String id = RequestUnit.getString("id");
		HashMap<String, Object> model = service.getModel(id);
		List<HashMap<String, Object>> list = service.getAttfileByCollectLogId(id);
		request.setAttribute("model", model);
		request.setAttribute("list", list);
		ModelAndView mav = new ModelAndView("/view/manager/info/details");
		
		//日常采集日志详情查询操作日志
		OperationLogModel entity = new OperationLogModel();
		entity.setOperationuserid(SessionUnit.getUserId());
		entity.setOperationcontent("日常采集日志详情查询");
		entity.setOperationname("日常采集日志详情查询");
		entity.setActionid("4");
		op_dao.insert(entity);
				
		return mav;
	}
}
